package com.groupseven.happycampus.controller;

import com.groupseven.happycampus.common.pojo.Consumer;
import com.groupseven.happycampus.common.result.R;
import com.groupseven.happycampus.common.utils.OssUtils;
import com.groupseven.happycampus.common.vo.LayuiPageVo;
import com.groupseven.happycampus.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

@RestController
@RequestMapping("/consumer")
public class ConsumerController {
    @Autowired
    private ConsumerService consumerService;

    @RequestMapping("/consumerList")
    public LayuiPageVo<Consumer> consumerInformationPageDate(@RequestParam Map<String, Object> params) {
        return consumerService.consumerInformationPageDate(params);
    }

    @RequestMapping("/addConsumer")
    public R addConsumer(Consumer consumer) {
        System.out.println(consumer);
        int result = consumerService.addConsumer(consumer);
        if (result==1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 实现消费者信息修改功能
     * @param consumer 传入修改页面提交的数据
     * @return 返回执行操作情况
     */
    @RequestMapping("/updateConsumerInformation")
    public R updateConsumerInformation(Consumer consumer) {
        System.out.println(consumer);
        int result = consumerService.updateConsumerInformation(consumer);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 实现图片上传功能
     * @param file 文件
     * @return 返回执行操作情况
     */
    @RequestMapping("/upload")
    public R upload(MultipartFile file) throws IOException {
        if (file!=null) {
            String originalFilename = file.getOriginalFilename();
            InputStream inputStream = file.getInputStream();
            String url = OssUtils.uploadFile(inputStream, originalFilename);
            //将 url 发给前端
            return R.ok().data("url", url);
        }
        return R.error();
    }

    /**
     *
     * @param id 传入要删除的 id
     * @return 返回执行操作情况
     */
    @RequestMapping("/deleteConsumer")
    public R deleteConsumer(Integer id) {
        System.out.println(id);
        int result = consumerService.deleteConsumer(id);
        if (result==1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 批量删除
     * @param ids 需要删除的 id
     * @return 返回执行操作情况
     */
    @GetMapping("/deletesConsumer")
    public R deletesConsumer(String ids) {
        System.out.println(ids);
        int result = consumerService.deletesConsumer(ids);
        if (result>0) {
            return R.ok();
        } else {
            return R.error();
        }
    }

//    /**
//     * 维修模块工种树状图
//     * @return 树状图需要的数据
//     */
//    @RequestMapping("/personnelBar")
//    public R echartBar() {
//        List<TypeWorkBarVo> list = consumerService.typeWorkBarData();
//        return R.ok().data("typeWorkBar", list);
//    }
}
